<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv=X-UA-Compatible content="IE=edge,chrome=1">
    <meta content=always name=referrer>
    <link rel="stylesheet" type="text/css" href="__STATIC__/css/style_user.css">
    <script src="__STATIC__/js/jquery-1.8.2.js"></script>
    <title>绑定小号手机</title>
</head>
{include file="public/href" /}
<body>
<form  action="" method="post" id="form" >
    <div class="wrap-pass">
        <div class="header">
            <p>绑定手机号</p>
        </div>
        <div class="center">
            <div class="title">
                <ul>
                    <li class="cir"></li>
                    <li>设置用户姓名</li>
                    <li class="cir"></li>
                </ul>
            </div>
            <div>
                <input class="text" type="text" placeholder="请输入用户姓名" datatype="*" name="nickname" nullmsg="请输入用户姓名" >
            </div>
            <div class="title">
                <ul>
                    <li class="cir"></li>
                    <li>设置绑定手机</li>
                    <li class="cir"></li>
                </ul>
            </div>
            <div>
                <input class="text" type="text" placeholder="请输入绑定的手机号" name="username" nullmsg="请输入绑定的手机号" datatype="m" errormsg="绑定手机号格式不正确">
            </div>
            <div class="yanzheng">
                <input class="text" type="text" name="yzm_code"  nullmsg="请输入验证码" datatype="n6" errormsg="请输入6位纯数字验证码" placeholder="验证码">
                <span><a href="javascript:check_num(2);" id="yzm_2">验证码</a></span>
            </div>

        </div>
        <input type="submit" class="submit" id="yes" value="确定" >
    </div>
</form>
{include file="public/src"  /}
<script>
    $(function () {
        $.Tipmsg.r = null;
        $.Tipmsg.p = null;
        $.Datatype.n6 = /^\d{6}$/;
        var demo = $("#form").Validform({
            tiptype: function (msg) {
                layer.open({content: msg, skin: 'msg', time:2});
            },
            btnSubmit:'#yes',
            tipSweep: true,
            ajaxPost:true,
            callback:function (t) {
                if (t.code != 200){
                    layer.open({content:t.message,skin:'msg',time:2});
                }else{
                    setTimeout(function () {
                        layer.open({content:t.message,skin:'msg',time:2});
                    },2000);
                    window.location.href = "{:url('mobile/user_port/index')}";
                }
            }

        });
        demo.config({
            url:"{:url('mobile/shop_set/bind_small_act')}",
            ajaxpost:{
                error: function () {
                    layer.open({content:'网络繁忙,请重试',skin:'msg',time:2});
                }
            }
        })
    });
    //发送验证码
    function check_num(id){
        var tel = $("[name='username']").val();
        if (tel == ''){
            layer.open({content: '请输入绑定手机号码', skin: 'msg', time:2});
            return false;
        }
        if (!(/^1(3|4|5|7|8)\d{9}$/.test(tel))){
            layer.open({content: '绑定手机号码格式不正确', skin: 'msg', time:2});
            return false;
        }
        if (!check_unique(tel)){
            return false;
        }

        count_down(id);
        $.ajax({
            url:"{:url('mobile/shop_set/get_code')}",
            dataType:'json',
            type:'post',
            data:{'tel':tel,'id':id,'act':'small'},
            success:function(t){
                if (t.code != 200){
                    layer.open({content: t.message,skin: 'msg',time:2});
                }
            },
            error: function () {
                layer.open({content:'网络繁忙,请重试',skin:'msg',time:2});
            }
        })
    }
    //验证码倒计时
    function count_down(id){
        var num = 60;
        var verif = $('#yzm_'+id);
        var timer = setInterval(function(){
            verif.text(num+'秒');
            if (num <=60 && num >= 1){
                verif.removeAttr('href');
            }else{
                verif.text('重新获取验证码');
                window.clearInterval(timer);
                verif.attr("href",'javascript:check_num(this.id);');
            }
            num--;
        },1000);
    }
    //检测新绑定的手机号码的唯一性
    function check_unique(tel){
        var flag = true;
        $.ajax({
            url:"{:url('mobile/shop_set/bind_phone')}",
            type:'post',
            async:false,
            dateType:'json',
            data:{'tel':tel},
            success:function(t){
                console.log(t.code)
                if (t.code != 200){
                    layer.open({content: t.message,skin: 'msg',time:2});
                    flag = false;
                }
            },
            error: function () {
                layer.open({content:'网络繁忙,请重试',skin:'msg',time:2});
            }
        });
        return flag;
    }
</script>
</body>
</html>